package game.module.badword.dao;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Files;

import game.module.template.BadWordTemplate;

public class BadWordTemplateCache {

	private static Logger logger = LoggerFactory.getLogger(BadWordTemplateCache.class);

	static class SingletonHolder {
		static BadWordTemplateCache instance = new BadWordTemplateCache();
	}

	public static BadWordTemplateCache getInstance() {
		return SingletonHolder.instance;
	}

	public List<BadWordTemplate> loadFromDb() {
		try {
			String jsonStr = Files.asCharSource(new File("data/MaskWord.json"), Charset.forName("UTF-8")).read();
			ObjectMapper mapper = new ObjectMapper();
			List<BadWordTemplate> jsonObj = mapper.readValue(jsonStr, new TypeReference<List<BadWordTemplate>>() {
			});
			logger.info("size={}", jsonObj.size());
			return jsonObj;
		} catch (Exception e) {
			logger.error("", e);
		}
		return null;
	}

	public static void main(String[] args) {
		BadWordTemplateCache badWordTemplateCache = new BadWordTemplateCache();
		List<BadWordTemplate> retList = badWordTemplateCache.loadFromDb();
		logger.info("retList={}", retList);
	}

}
